<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>该校暂未开启团报入口</title>
    <style>
        table{border: 1px solid #333333;}
        table td{border: 1px solid #333333;}
    </style>
</head>
<body>
    <div id="topic-table" class="topic-table"></div>
    <script>
        var data = [
            { name: '材料题',topicType:'TOPIC_TYPE_CASE',difficulty:'DIFFICULT_TYPE_EASY',topicList:[
                { name: '材料题-判断题-1',topicType:'TOPIC_TYPE_TRUE_FALSE',difficulty:'DIFFICULT_TYPE_EASY'},
                { name: '材料题-单选-2',topicType:'TOPIC_TYPE_RADIO',difficulty:'DIFFICULT_TYPE_MIDDLE'},
                { name: '材料题-单选-1',topicType:'TOPIC_TYPE_RADIO',difficulty:'DIFFICULT_TYPE_EASY'},
            ]},
            { name: '单选2',topicType:'TOPIC_TYPE_RADIO',difficulty:'DIFFICULT_TYPE_MIDDLE'},
            { name: '单选1',topicType:'TOPIC_TYPE_RADIO',difficulty:'DIFFICULT_TYPE_EASY'},
            { name: '判断题3',topicType:'TOPIC_TYPE_TRUE_FALSE',difficulty:'DIFFICULT_TYPE_DIFFICULT'},
            { name: '判断题2',topicType:'TOPIC_TYPE_TRUE_FALSE',difficulty:'DIFFICULT_TYPE_MIDDLE'},
            { name: '判断题1',topicType:'TOPIC_TYPE_TRUE_FALSE',difficulty:'DIFFICULT_TYPE_EASY'},
           
        ];

        let diff = ['DIFFICULT_TYPE_EASY','DIFFICULT_TYPE_MIDDLE','DIFFICULT_TYPE_DIFFICULT']
        , topicTypeSort = ['TOPIC_TYPE_RADIO','TOPIC_TYPE_MULTIPLE','TOPIC_TYPE_UNDEFINED','TOPIC_TYPE_TRUE_FALSE','TOPIC_TYPE_FILLING','TOPIC_TYPE_ANSWER','TOPIC_TYPE_CASE']

        //设置排序标识
        let sortFlag = function(data,pTypeSort){
            data.forEach(function(item){
                //题目类型排序标识
                item.typeSort = topicTypeSort.indexOf(item.topicType) + 1;
                //题目难易程度排序
                item.diffSort = diff.indexOf(item['difficulty']) + 1;
                if(item.topicList && Array.isArray(item.topicList) && item.topicList.length > 0){
                    sortFlag(item.topicList);
                }
            })
        }

        sortFlag(data);
       
        let sort = function(data){
            data.sort(function(a,b){
                let flag = 0;
                if(a.topicType == 'TOPIC_TYPE_CASE'){
                    sort(a.topicList)
                }
                if(b.topicType == 'TOPIC_TYPE_CASE'){
                    sort(b.topicList)
                }
                if(a.typeSort == b.typeSort){
                    flag = a.diffSort - b.diffSort
                }else{
                    flag = a.typeSort - b.typeSort
                }
                return flag;
            })
        }

        sort(data);

        let dataStruct = function(data,saveObj){
            data.forEach(function(item){
                let topicType = item['topicType']
                , diff = item['difficulty']
                , topicTypes = saveObj[topicType] || (saveObj[topicType] = {});
                if(topicType == 'TOPIC_TYPE_CASE'){
                    let topicList = topicTypes.topicList || (topicTypes.topicList = {});
                    dataStruct(item.topicList,topicList)
                }else{
                    (topicTypes[diff]||(topicTypes[diff] = [])).push(item)
                }
            });
            return saveObj;
        };

        let newData = dataStruct(data,{});

        console.log(newData)
        
        let flatObj = function(data){
            let target = {}
            let setItem = function(data,name){
                Object.keys(data).forEach(function(key,i){
                    //将key值存储到target对象中
                    let value = data[key];
                    if(typeof value == 'object'){
                        key = !name && key || name+'['+key+']';
                        setItem(value,key);
                    }else{
                        key = !name && key || name+'['+key+']';
                        target[key] = value
                    }
                })
            };
            setItem(data);
            return target;
        } 

        let setMergeData = function(data,prevStarRow){
            prevStarRow = prevStarRow || 0;
            let mergeData = {};
            for(let topicType in data){
                if(topicType == 'TOPIC_TYPE_CASE'){
                    mergeData['TOPIC_TYPE_CASE'] = setMergeData(data[topicType].topicList,prevStarRow)
                }else{
                    mergeData[topicType] = {
                        startRow:prevStarRow
                        ,endRow:prevStarRow + Object.keys(data[topicType]).length-1
                    }
                    prevStarRow = mergeData[topicType].endRow+1
                }
            }
            return mergeData;
        }

        let mergeData = setMergeData(newData);

        console.log(mergeData)

        // for(let type in mergeData){
        //     let item = mergeData[type]
        //     if(type == 'TOPIC_TYPE_CASE'){
        //         let attrs = flatObj(item);
        //         let startMin = [] , endMax = [];
        //         for(let attr in attrs){
        //             if(attr.indexOf('startRow') > -1){
        //                 startMin.push(attrs[attr])
        //             }
        //             if(attr.indexOf('endRow') > -1){
        //                 endMax.push(attrs[attr])
        //             }
        //         }
        //         let startRow = Math.min.apply(null,startMin);
        //         let endRow = Math.max.apply(null,endMax);
        //     }
        // }


        let expr = 'TOPIC_TYPE_CASE.topicList.TOPIC_TYPE_RADIO.DIFFICULT_TYPE_EASY';
        let getArgs = function(expr){
            let args = []
            expr.split('.').forEach(function(arg){
                args.push('["'+arg+'"]')
            })
            return args.join('');
        }
        let getVal = function(data,expr){
            expr.split(".").forEach(function(arg){
                data = data[arg]
            })
            return data;
        }
        let deleteEmptyObj = function(data,expr){
            let args = getArgs(expr);
            eval('delete data'+args);
            expr = expr.substring(0,expr.lastIndexOf("."));
            if(JSON.stringify(getVal(data,expr)) == '{}'){
                deleteEmptyObj(data,expr)
            }
        }
        
        deleteEmptyObj(newData,expr);

        console.log(newData);
    </script>
    <script src="./table.js"></script>
    <script>
        // let table = document.querySelector('table')
        // tableUtils.mergeRowByCol(table,0,1,3);
        //tableUtils.getCells(table,-1,-1,1);
       // tableUtils.mergeRowByContent(table,1)

       var obj = {
           'school[0][name]':'国防'
           ,'name[0]':'陈康'
           ,'name[1]':'ck'
           ,'age':12
       };

        
       console.log()
    </script>
</body>
</html>